<?php

/*
 * system_module.php
 * author Surabhi Shukla
 * @link http://www.smartdatainc.net
 * @version 0.0.1
 */
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

    public $name = 'User';
    var $belongsTo = array(
        'Role' => array(
            'className' => 'Role',
            'foreignkey' => 'role_id',
        )/*,
        'ServiceUnit' => array(
            'className' => 'ServiceUnit',
            'foreignkey' => 'service_unit_id',
        ),*/
    );
    var $hasMany = array(
        'UserServiceUnit' => array(
            'className' => 'UserServiceUnit',
            'foreignkey' => 'service_unit_id',
        ),/*
        'ServiceUnit' => array(
            'className' => 'ServiceUnit',
            'foreignkey' => 'service_unit_id',
        ),*/
        'Log' => array(
            'className' => 'Log',
            'foreignKey' => 'user_id',
            'order' => 'Log.created DESC',
            'limit' => 1
        ),
    );
    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Username is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Password is required'
            )
        ),
        'email' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Email is required'
            )
        ),
        'e_name' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'E Name is required'
            )
        ),
        'c_name' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'C Name is required'
            )
        ),
        'role_id' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Please select Role'
            )
        )
        ,
         /* 'service_unit_id' => array(
            'required' => array(
                'rule' => 'checkSelection',
                'message' => 'Please select Service Unit'
            )
        )
            ,
           'service_group_id' => array(
              'required' => array(
              'rule' => array('notEmpty'),
              'message' => 'Please select Group Unit'
              )
              ) */
    );
    
    /*public function checkSelection($data){
        $data  = array_shift($data);
        
        if(count($data)){
            return true;
        } else{
            return false;
        }
    }*/
    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $passwordHasher = new BlowfishPasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                    $this->data[$this->alias]['password']
            );
        }
        return true;
    }

}

?>